package com.wang.API;

import com.wang.bean.Result;
import com.wang.bean.SysFile;
import com.wang.service.SysFileService;
import com.wang.util.FileUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@RestController
@RequestMapping("api")
public class FileController {

    @Autowired
    private SysFileService sysFileService;

    private String publicfilePath = "/local/image/";

    @RequestMapping(value = "/uploadImage")
    @RequiresPermissions("sys:user:uploadImage")
    public Result uploadImage(MultipartFile file){

        Result result = new Result();
        List<Result> rs = new ArrayList<Result>();

        if (file.isEmpty()){
            result.setCode("1001");
            return result;
        }

        System.out.println(FileUtil.getFilePath());

        String filename = file.getOriginalFilename();
        String upFileName = filename.substring(0, filename.lastIndexOf("."));
        System.out.println(upFileName);
        String suffixName = filename.substring(filename.lastIndexOf(".") + 1);
        System.out.println(suffixName);

        List<String> filType= Arrays.asList("gif","bmp","jpg","png","jpeg","webp");
        if (!filType.contains(suffixName)){
            result.setCode("1002");
            return result;
        }

        if (file.getSize() > 20*1024*1024){
            result.setCode("1003");
            return result;
        }

        String fileName = UUID.randomUUID().toString();
        String filePathName = FileUtil.getFilePath() + publicfilePath + fileName + "." + suffixName;
        File dest = new File(filePathName);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e){
            result.setCode("1004");
            return result;
        }

        SysFile sysFile = new SysFile();
        sysFile.setFile_name(fileName);
        sysFile.setFile_upname(upFileName);
        sysFile.setFile_path(publicfilePath);
        sysFile.setFile_type(suffixName);
        sysFile.setFile(publicfilePath + fileName + "." + suffixName);
        sysFile.setUpload_time(new Date());
        sysFile.setFile_function("题库");
        sysFile.setStatus(1);
        sysFileService.uploadSysFile(sysFile);
        SysFile sysFileByFileName = sysFileService.findSysFileByFileName(fileName);

        result.setCode("1005");
        result.setData(sysFileByFileName.getFile_name());
        return result;
    }

    @RequestMapping(value = "/downloadImage")
    @RequiresPermissions("sys:user:downloadImage")
    public Result downloadImage(String fileName, HttpServletRequest request , HttpServletResponse response) throws UnsupportedEncodingException {

        Result result = new Result();

        if (fileName == null || "".equals(fileName)){
            result.setCode("1006");
            return result;
        }

        //FileUtil.downLoadMp4(response,fileName);
        SysFile sysFileByFileName = sysFileService.findSysFileByFileName(fileName);

        if (sysFileByFileName == null){
            result.setCode("1007");
            return result;
        }

        String file = sysFileByFileName.getFile();

        String file_url = FileUtil.getFilePath() + file;
        FileInputStream fis = null;
        ServletOutputStream outputStream = null;
        //设置下载文件名
        response.setHeader("content-disposition", "attachment;filename=" + sysFileByFileName.getFile_name());
        //提示下载图片
        response.setHeader("content-type", "image/jpeg");
        try {
            fis = new FileInputStream(file_url);
            outputStream = response.getOutputStream();
            int len = 1;
            byte[] b = new byte[1024];
            while((len=fis.read(b))!=-1){
                outputStream.write(b, 0, len);
            }
            fis.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            result.setCode("1008");
            return result;
        } catch (IOException e) {
            result.setCode("1009");
            return result;
        }

        try {
            fis.close();
            outputStream.close();
        } catch (Exception e){
            result.setCode("1013");
            return result;
        }

        result.setCode("1010");
        return result;
    }

    /*
    @RequestMapping(value = "/uploadExcel")
    //@RequiresPermissions("sys:user:uploadImage")
    public Result uploadExcel(MultipartFile file){
        Result result = new Result();
        return result;
    }
    */
}
